var db = require('../model/db')
var middleware = require('./middlewareCtrl')
// 导入用于生成jwt字符串
var jwts = require('jsonwebtoken')
var config = require('../config')
// 注册
module.exports.Reg = (req, res) => {
  const { account_number, password } = req.body
  const selectSql = `SELECT * FROM user WHERE account_number=?`
  db.dbSql(selectSql, [account_number], (err, results) => {
    if (err) return res.send({ code: 102, msg: '查询失败' });
    if (results.length == 0) {
      // 插入新的用户sql语句
      const insertSql = `INSERT INTO user (account_number, password) VALUES (?, ?)`
      // 调用db,query执行语句
      db.dbSql(insertSql, [account_number, password], (err, results) => {
        // 判断sql语句是否成功
        console.log(account_number, password);
        if (err) return res.send({ code: 105, msg: '新增失败' });
        // 注册成功
        res.send({ code: 200, msg: '' })
      })
    } else {
      // 注册失败
      res.send({ code: 102, msg: '账号重复' })
    }
  })
}
// 登录
module.exports.Login = (req, res) => {
  var userifno = req.body
  var sql = `select * from user where account_number=? and password=? and user_state='1' `
  db.dbSql(sql, [userifno.account_number, userifno.password], (err, results) => {
    if (err) {
      return res.send({ code: 102, msg: '查询失败' })
    } else {
      if (results.length != 0) {
        // 获得用户所有信息
        // SELECT * from user, role where user.role_id = role.role_id and user.user_id = ${ results[0].user_id } and role.role_id = ${ results[0].role_id };
        var sql2 = `SELECT * from user, role where user.role_id = role.role_id and user.user_id = ${results[0].user_id} and role.role_id = ${results[0].role_id}; UPDATE user set login_time='${new Date().toISOString().slice(0, 19).replace('T', ' ')}' where user_id='${results[0].user_id}';`
        // 调用db,query执行语句
        db.dbSql(sql2, [], (err, results) => {
          // 判断sql语句是否成功
          if (err) return res.send({ code: 102, msg: '查询失败' + err });
          //  在服务端生成token
          // 参数一：用户信息对象
          // 参数二：加密密钥
          // 参数三：配置对象的token有效期
          console.log(results[0][0].user_id);
          const tokenStr = jwts.sign({ user_id: results[0][0].user_id }, config.token.jwtSecretKey, {
            expiresIn: config.token.expiresIn
          })
          res.send({ code: 200, token: 'Bearer ' + tokenStr, data: results[0][0], role: results[0][0].role_name })
        })
      } else {
        return res.send({ code: 102, msg: '未查到账号或者被冻结' })
      }
    }
  })

}



// 查看所有用户信息
module.exports.showInfo = function (req, res) {
  const roleId = 5 // 假设需要查询的角色 ID 为 5
  const sql = `SELECT * FROM user WHERE role_id = ?`
  db.dbSql(sql, [roleId], (err, results) => {
    if (err) return res.send({ code: 102, msg: '查询失败' })
    res.send({ code: 200, data: results })
  })
}
// 查看管理员用户信息
module.exports.showAdmin = function (req, res) {
  const roleId = 5 // 假设需要排除的角色 ID 为 5
  const sql = `SELECT * FROM user WHERE role_id != ?`
  db.dbSql(sql, [roleId], (err, results) => {
    if (err) return res.send({ code: 102, msg: '查询失败' })
    res.send({ code: 200, data: results })
  })
}
// 角色列表信息
module.exports.showRoleList = function (req, res) {
  const sql = `SELECT * FROM role`
  db.dbSql(sql, [], (err, results) => {
    if (err) return res.send({ code: 102, msg: '查询失败' })
    res.send({ code: 200, data: results })
  })
}

// 修改个人信息
module.exports.updateUser = function (req, res) {
  var data = req.body
  middleware.updateById('user', data, 'user_id', res)
}
// 查看个人用户信息
module.exports.showuser = function (req, res) {
  const roleId = 5 // 假设需要查询的角色 ID 为 5
  const sql = `SELECT * FROM user WHERE role_id = ? and user_id=?`
  db.dbSql(sql, [roleId, req.user.user_id], (err, results) => {
    if (err) return res.send({ code: 102, msg: '查询失败' })
    res.send({ code: 200, data: results })
  })
}